Ext.onReady(function() {
	
	var newWin = new Ext.Window({
		title: '内容编辑',
        layout:'fit',
        applyTo:'ContentWin',
        width:900,
    	height:520,
        closeAction:'hide',
        plain: true
    });
	
	var editForm = new Ext.form.FormPanel({
		labelAlign: 'right',
		region: 'center',
		labelWidth: 100,
		frame: true,
		xtype: 'fieldset',
		items: [
			{
				items: [
					{	
						columnWidth: .01, layout: 'form',
		            	items: [
		            	        { xtype: 'hidden', name: 'id', hidden:true, hiddenLabel:true}
		            	] 
			        }
				]
			},
			{
				items: [
					 {	
					 	layout: 'form',
		            	items: [
		            	        { xtype: 'textfield', name: 'title', fieldLabel: '标题',allowBlank : false,anchor : '95%'}
		            	]
		            },
		            {	
					 	layout: 'form',
		            	items: [
		            	        { xtype: 'textfield', name: 'displayOrder', fieldLabel: '排序号',allowBlank : false,anchor : '95%'}
		            	]
		            },
		            {	layout: 'form',
		            	items: [
		            	        { xtype: 'textfield', name: 'remark', fieldLabel: '备注 ',anchor : '95%'}
		            	]
		            }
				]
			}
		],
		buttons: [
			{
				text: '保存'	,
				handler: function() {
					if(!editForm.getForm().isValid())
						return;
									
					editForm.form.doAction('submit', {
						url: '/MeowcashVisa/admin/visa/KnowledgeAction/save.action',
						method: 'post',
						params: '',
						success: function(form, action) {
							if(action.result.result == 'success') {
								Ext.MessageBox.alert('结果', '保存成功！');
								form.reset();
								grid.getStore().reload();
								editWin.hide();
							}
						}
					});
				}
			},
			{
				text: '取消'	,
				handler: function() {
					editForm.form.reset();
					editWin.hide();
				}
			}
		]
	});
	
	var editWin = new Ext.Window({
		title: '编辑常识',
        layout:'fit',
        width:450,
    	height:180,
        closeAction:'hide',
        plain: true,
        layout: 'border',
        items: [editForm]
    });
	
	var searchForm = new Ext.FormPanel({
		frame: true,
		region: 'north',
		labelAlign: 'right',
		items: [
			{
				layout: 'column',
				items: [
					{
						columnWidth: .33, layout: 'form',
		            	items: [
		            	     { xtype: 'textfield', name: 'title', fieldLabel: '标题',anchor : '95%'}
		            	]
					},
					{
						columnWidth: .33, layout: 'form',
		            	items: [
		            	     { xtype: 'textfield', name: 'remark', fieldLabel: '备注',anchor : '95%'}
		            	]
					}
				]
			}
		],
		buttons: [
			new Ext.Button({
				text: '查询',
				width: 70,
				handler: function() {
					var fv = searchForm.getForm().getValues();
					ds.baseParams= fv;
					ds.load({params: {start:0, limit:20}});
				}
			}),
			new Ext.Button({
				text: '清空',
				width: 70,
				handler: function() {
					searchForm.form.reset();
					ds.baseParams= {};
					ds.load({params: {start:0, limit:20}});
				}
			}),
			new Ext.Button({
				text: '新增',
				width: 70,
				handler: function() {
					editForm.form.reset();
					editWin.show();
				}
			})
		]
	});
	
	searchForm.render('north-div');
				
	var cm = new Ext.grid.ColumnModel([
		new Ext.grid.RowNumberer(),
		{header:'标题', dataIndex:'title', sortable:true},
		{header:'排序号', dataIndex:'displayOrder', sortable:true},
		{header:'备注', dataIndex:'remark', sortable:true},
	    {header:'创建时间', dataIndex:'createTime', sortable:true},
	    {header:'操作', dataIndex:'op', renderer: renderOp,width:100, align:'left'}
	]);
	
	function renderOp(value, cellmeta, record, rowIndex, columnIndex, stor) {
		var id = record.data.id;
		var delInfo = record.data.title; 
		var editStr = '<img alt=\"编辑\" src=\"/MeowcashVisa/admin/images/edit.gif\" style=\"cursor: pointer;\" onclick=\"showInfo(\''+id+'\');\">';
		var detailStr = '<img alt=\"内容\" src=\"/MeowcashVisa/admin/images/copy.gif\" style=\"cursor: pointer;\" onclick=\"showDetail(\''+id+'\');\">';
		var delStr = '<img alt=\"删除\" src=\"/MeowcashVisa/admin/images/del.gif\" style=\"cursor: pointer;\" onclick=\"deleteUser(\''+id+'\', \'' + delInfo + '\');\">';
		
		var preStr = '<img alt=\"编辑\" src=\"/MeowcashVisa/admin/images/find.gif\" style=\"cursor: pointer;\" onclick=\"showPreView(\''+id+'\');\">';
		
		return editStr + '&nbsp;&nbsp;&nbsp;&nbsp;' + detailStr + '&nbsp;&nbsp;&nbsp;&nbsp;' + delStr + '&nbsp;&nbsp;&nbsp;&nbsp;' + preStr;
	}
	
	window.showPreView = function(id) {
		var url = '/MeowcashVisa/admin/visa/ContentEditorAction/showPreview.action?tableName=knowledge&id=' + id;
	    newWin.update({
	    	title: '内容预览',
	    	html: '<iframe scrolling="auto" frameborder="0" width="100%" height="100%" src="'+ url + '"></iframe>'
	    });
		newWin.show();
	};
	
	window.showInfo = function(id) {
		editForm.load({
			url: '/MeowcashVisa/admin/visa/KnowledgeAction/getDetailInfo.action',
			params: {knowledgeId: id}
		});
		editWin.show();
	};
	
	window.showDetail = function(id) {
	    var url = '/MeowcashVisa/admin/visa/ContentEditorAction/showEditor.action?tableName=knowledge&id=' + id;
	    newWin.update({
	    	title: '内容编辑',
	    	html: '<iframe scrolling="auto" frameborder="0" width="100%" height="100%" src="'+ url + '"></iframe>'
	    });
		newWin.show();
	};
	
	window.deleteUser = function(id, delInfo) {
		Ext.MessageBox.confirm('提示', '确定删除  ' + delInfo + ' ？', function(btn) {
			if(btn != 'yes') {
				return;
			}
			
			Ext.Ajax.request({
				method: 'post',
				url: '/MeowcashVisa/admin/visa/KnowledgeAction/delete.action',
				params: {knowledgeId: id},
			   	success:function(resp){
			    	var obj=Ext.util.JSON.decode(resp.responseText);
			      	if(obj.result == 'success') {
			      		grid.getStore().reload();
			      		Ext.MessageBox.alert('提示', '删除成功！');
			      	}
			      	else {
			      		Ext.MessageBox.alert('报错了！！！', '删除失败！！！' + obj.info);
			      	}
			    }
			});
		});
	};
	
	var ds = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({url: '/MeowcashVisa/admin/visa/KnowledgeAction/search.action'}),
		remoteSort: false,
		reader: new Ext.data.JsonReader(
			{
				totalProperty: 'total',
				idProperty:'id',
				root: 'invdata'
			},
			[
				{name: 'id'},
				{name: 'title'},
				{name: 'createTime'},
				{name: 'createUser'},
				{name: 'modifyTime'},
				{name: 'modifyUser'},
				{name: 'remark'},
				{name: 'displayOrder'}
			]
		)
	});

	ds.load({params: {start:0, limit:20}});
	
	var grid = new Ext.grid.GridPanel({
		el: 'grid',
		region: 'center',
		ds: ds,
		cm: cm,
	    viewConfig: {
	    	forceFit: true
	    },
	    bbar: new Ext.PagingToolbar({
		    pageSize: 20,
		    store: ds,
			displayInfo: true,
			displayMsg: '显示第{0}条到{1}条记录,一共{2}条',
			emptyMsg: '没有记录'
	    })
	});
	
	new Ext.Viewport({
		layout: 'border',
		items:[
		       searchForm,grid
		]
	});
});